<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="com.ignou.mca.beans.UserVO"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.ignou.mca.beans.QuestionsBean"%>
<%@page import="java.util.StringTokenizer"%>
<%@page import="java.util.Iterator"%>
<%@page import="com.ignou.mca.beans.ExamBean"%>
<%@page import="com.ignou.mca.datasource.DatabaseOperations"%><html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="theme/style.css"
	media="screen" />
<title>Examination</title>
<script type="text/javascript"> 
var timer = {
		minutes : 0,
		seconds : 0,
		elm : null,
		samay : null,
		sep : ':',
		init : function(m, s, elm) {
			m = parseInt(m, 10);
			s = parseInt(s, 10);
			if (m < 0 || s < 0 || isNaN(m) || isNaN(s)) {
				alert('Invalid Values');
				return;
			}
			this.minutes = m;
			this.seconds = s;
			this.elm = document.getElementById(elm);
			timer.start();
		},
		start : function() {
			this.samay = setInterval((this.doCountDown), 1000);
		},
		doCountDown : function() {
			if (timer.seconds == 0) {
				if (timer.minutes == 0) {
					clearInterval(timer.samay);
					timerComplete();
					return;
				} else {
					timer.seconds = 60;
					timer.minutes--;
				}
			}
			timer.seconds--;
			timer.updateTimer(timer.minutes, timer.seconds);
		},
		updateTimer : function(min, secs) {
			min = (min < 10 ? '0' + min : min);
			secs = (secs < 10 ? '0' + secs : secs);
			(this.elm).innerHTML = "Time left: <b>"+min + (this.sep) + secs+"</b>";
		}
	}
	function timerComplete() 
	{
		//alert('Exam Time is up click on end exam to get result');
		document.fm.submit(); 
	}
	
	function startExam(mins,secs)
	{
		for(var i=0;i<document.forms['fm'].elements.length;i++)
		{
			var str = document.forms['fm'].elements[i].name;
			if(str == "startButton")
			{
				document.forms['fm'].elements[i].disabled = true;
			}else
			{
				document.forms['fm'].elements[i].disabled = false;
			}
		}
		timer.init(mins,secs,'container');
	}
	
	function endExam(noOfQues)
	{
		var answers = [];
		
		for (var x = 1; x <= noOfQues; x ++) 
        {
			var optName = "option"+x;
			var radioButtons = document.getElementsByName(optName);
	        for (var xx = 0; xx < radioButtons.length; xx++) 
	        {
	            if(radioButtons[xx].checked)
            	{
	            	answers.push(radioButtons[xx].value);
            	}
	       	}
        }
		document.fm.selectedAnswers.value = answers.join('| ');	    
	}
	
	function init()
	{
		for(var i=0;i<document.forms['fm'].elements.length;i++)
		{
			var str = document.forms['fm'].elements[i].name;
			if(str!= "startButton")
			{
				document.forms['fm'].elements[i].disabled = true;
			}else
			{
				document.forms['fm'].elements[i].disabled = false;
			}
		}
	}
</script>

</head>
<body onload="javascript:window.history.forward(1);init()">
	<div id="MainWrapper">
		<div id="MainContent">
		<%
			UserVO uvo = (UserVO) session.getAttribute("UserVO");
			if(uvo==null)
			{
				response.sendRedirect("./Login.jsp");
			}
			else
			{
				String e = request.getParameter("exam");
				int examId = 0;
				if(e!=null)
				{
					examId = Integer.parseInt(e);					
				}
				
				ExamBean examDetails = (ExamBean) session.getAttribute("ExamDetails");
				%>
				<center><h2><%=examDetails.getExamName() %> Exam</h2><hr>
					<table>
						<tr><td>Exam Id: </td><td><%=examDetails.getExamId() %> </td><td></td><td>Exam Name: </td><td><%=examDetails.getExamName() %></td></tr>
						<tr><td>Exam Duration: </td><td><%=examDetails.getDuration() %> Minute(s)</td><td></td><td>Number of Questions: </td><td><%=examDetails.getNoOfQuestions() %></td></tr>
					</table>
				</center>
				<div align="right" id="container"></div>
				<hr>
				
					<form action="EndExam.do" method="post" name="fm" onsubmit="endExam(<%=examDetails.getNoOfQuestions()%>)">
					<table>
				<%
					ArrayList<QuestionsBean> ques = (ArrayList<QuestionsBean>) session.getAttribute("ExamQuestions");
					if(ques.size()>0)
					{
						Iterator<QuestionsBean> itr = ques.iterator();
						int quesNo = 1;
						while(itr.hasNext()) 
						{
							QuestionsBean qb = (QuestionsBean)itr.next();
							%>
							<tr><td>Question:<%=quesNo %></td><td><b><%=qb.getQuestion() %></b></td></tr>
							<%	
								ArrayList<String> st = qb.getOptions();
								Iterator<String> it = st.iterator();
								while(it.hasNext()) 
								{
									String option = (String) it.next();
									out.println("<tr><td></td><td><input type='radio' name='option"+quesNo+"' value='" + option+"'>" + option+"</input></td></tr>");
								}
							%>
							<%
							quesNo++;
						}
					}else
					{
						
					}
			%>
			
			</table>
				<input type="hidden" name="exam" value="<%=examId%>"/>
				<input type="hidden" name="selectedAnswers"/>
				<input type="hidden" name="subjectId" value="<%=examDetails.getSubjectId()%>"/>
				<input type="hidden" name="userName" value="<%=uvo.getUserName()%>"/>
				<center>
					<input type="button" name="startButton" value="Start Exam" onclick="javascript: startExam(<%=examDetails.getDuration()%>,0)"/>
					<input type="submit" name="submit" value="End Exam" disabled="disabled"/>
				</center>
			</form>
		
		<%
			}
		%>
		</div>
	</div>
</body>
</html>